package com.xjrsoft.module.customer.erp_model.eRP_FinanceIncome.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceIncome.dto.ErpFinanceincomeDto;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceIncome.dto.SaveErpFinanceincomeFormDataDto;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceIncome.vo.ErpFinanceincomeFormDataVo;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceIncome.vo.ErpFinanceincomeListVo;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceIncome.dto.ErpFinanceincomeListDto;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceIncome.vo.ErpFinanceincomebillVo;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceIncome.entity.ErpFinanceincomebill;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceIncome.dto.ErpFinanceincomebillDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import com.xjrsoft.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceIncome.entity.ErpFinanceincome;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceIncome.vo.ErpFinanceincomeVo;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceIncome.service.IErpFinanceincomeService;

/**
 *  控制器
 *
 * @author pp
 * @since 2021-09-23
 */
@RestController
@AllArgsConstructor
@RequestMapping("/erp_financeincome")
@Api(value = "其他收入", tags = "其他收入接口")
public class ErpFinanceincomeController {


	private final IErpFinanceincomeService erpFinanceincomeService;


	/**
	 * 详情
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入erpFinanceincome")
	public Response<ErpFinanceincomeFormDataVo> getFormData(@PathVariable String id) {
		ErpFinanceincomeFormDataVo formDataVo = new ErpFinanceincomeFormDataVo();
		// 主表数据
		ErpFinanceincome erpFinanceincome = erpFinanceincomeService.getById(id);
		formDataVo.setErpFinanceincomeVo(BeanUtil.copy(erpFinanceincome, ErpFinanceincomeVo.class));
		// 从表数据
		formDataVo.setErpFinanceincomebillVoList(BeanUtil.copyList(erpFinanceincomeService.getErpFinanceincomebillByParentId(id), ErpFinanceincomebillVo.class));
		return Response.ok(formDataVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入erpFinanceincome")
	public Response<PageOutput<ErpFinanceincomeListVo>> getPageList(ErpFinanceincomeListDto listDto) {
		IPage<ErpFinanceincome> page = erpFinanceincomeService.getPageList(listDto);
		List<ErpFinanceincomeListVo> records = BeanUtil.copyList(page.getRecords(), ErpFinanceincomeListVo.class);
		// 转换列表数据
		DataTransUtil.transListShowData(records);
		// 工作流数据处理
		FlowableUtil.systemFormList(records, SecureUtil.getModuleId());
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入erpFinanceincome")
	public Response save(@RequestBody SaveErpFinanceincomeFormDataDto formDto) {
		ErpFinanceincomeDto erpFinanceincomeDto = formDto.getErpFinanceincomeDto();
		ErpFinanceincome erpFinanceincome = BeanUtil.copy(erpFinanceincomeDto, ErpFinanceincome.class);
		List<ErpFinanceincomebillDto> erpFinanceincomebillDtoList = formDto.getErpFinanceincomebillDto();
		List<ErpFinanceincomebill> erpFinanceincomebillList = BeanUtil.copyList(erpFinanceincomebillDtoList, ErpFinanceincomebill.class);
		String dataId = erpFinanceincomeService.addErpFinanceincome(erpFinanceincome, erpFinanceincomebillList);
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入erpFinanceincome")
	public Response update(@PathVariable String id, @RequestBody SaveErpFinanceincomeFormDataDto formDto) {
		ErpFinanceincomeDto erpFinanceincomeDto = formDto.getErpFinanceincomeDto();
		ErpFinanceincome erpFinanceincome = BeanUtil.copy(erpFinanceincomeDto, ErpFinanceincome.class);

		List<ErpFinanceincomebillDto> erpFinanceincomebillDtoList = formDto.getErpFinanceincomebillDto();
		List<ErpFinanceincomebill> erpFinanceincomebillList = BeanUtil.copyList(erpFinanceincomebillDtoList, ErpFinanceincomebill.class);
		return Response.status(erpFinanceincomeService.updateErpFinanceincome(id, erpFinanceincome, erpFinanceincomebillList));
	}


	
	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(erpFinanceincomeService.removeByIds(Func.toStrList(ids)));
	}

	
}
